package com.xauto.pioneer.appuser.activity.parking;

import android.app.AlertDialog;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.util.Log;
import android.view.View;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;

import com.xauto.pioneer.appuser.R;
import com.xauto.pioneer.appuser.activity.BaseActivity;
import com.xauto.pioneer.appuser.adapter.ParkingRentRulesAdapter;
import com.xauto.pioneer.appuser.http.BaseLHttpHandler;
import com.xauto.pioneer.appuser.http.LHttpLib;
import com.xauto.pioneer.appuser.model.JSONStatus;
import com.xauto.pioneer.appuser.model.MonthlyRentRuleInfo;
import com.xauto.pioneer.appuser.util.LicenseKeyboardUtil;
import com.xauto.pioneer.appuser.view.PromptDialog;
import com.xauto.pioneer.appuser.view.TopBar;

import org.json.JSONArray;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.OnClick;

import static com.xauto.pioneer.appuser.activity.parking.InputCarNumberActivity.INPUT_LICENSE_COMPLETE;
import static com.xauto.pioneer.appuser.activity.parking.InputCarNumberActivity.INPUT_LICENSE_KEY;

/**车辆包月
 * Created by Administrator on 2018/5/24.
 */

public class MonthlyPayActivity extends BaseActivity {
    @Bind(R.id.ac_monthlypay_topBar)
    TopBar ac_monthlypay_topBar;
    @Bind(R.id.lv_monthlypay)
    ListView lv_monthlypay;
    String parking_id;
    List<MonthlyRentRuleInfo> list;

    private PromptDialog promptDialog;
    ParkingRentRulesAdapter adapter;
    String car_no = "陕A11111";



    /**切换车牌  0--普通车牌   1---新能源车牌*/
    public static int  INPUT_CARNUM = 0;
    /**自定义键盘相关*/
    private LicenseKeyboardUtil keyboardUtil;
    @Bind(R.id.et_inputbox1)
    EditText inputbox1;
    @Bind(R.id.et_inputbox2)
    EditText inputbox2;
    @Bind(R.id.et_inputbox3)
    EditText inputbox3;
    @Bind(R.id.et_inputbox4)
    EditText inputbox4;
    @Bind(R.id.et_inputbox5)
    EditText inputbox5;
    @Bind(R.id.et_inputbox6)
    EditText inputbox6;
    @Bind(R.id.et_inputbox7)
    EditText inputbox7;
    @Bind(R.id.et_inputbox8)
    EditText inputbox8;
    @Bind(R.id.tv_switchcartype)
    TextView tv_switchcartype;
    @Bind(R.id.tv_divider7)
    TextView tv_divider7;

    IntentFilter finishFilter;
    BroadcastReceiver receiver;
    AlertDialog alertDialog;
    AlertDialog.Builder builder;



    @Override
    protected int getContentView() {
        return R.layout.ac_monthlypayrule;
    }

    @Override
    protected void initView() {
        ac_monthlypay_topBar.showView(TopBar.LEFT_IMAGE_BTN, TopBar.TITLE);
        ac_monthlypay_topBar.setImage(TopBar.LEFT_IMAGE_BTN, R.mipmap.ic_common_left);
        ac_monthlypay_topBar.setText(TopBar.TITLE, R.string.mine_monthlypay);
        ac_monthlypay_topBar.setLeftOnClick(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onBackPressed();
            }
        });

        keyboardUtil = new LicenseKeyboardUtil(this,new EditText[]{inputbox1,inputbox2,inputbox3,
                inputbox4,inputbox5,inputbox6,inputbox7,inputbox8});
        keyboardUtil.showKeyboard();


        promptDialog = new PromptDialog(mContext, getString(R.string.notice), getString(R.string.text_not_network),
                getString(R.string.to_setting), getString(R.string.logout), getString(R.string.dialogpic_notice),new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //去设置
                promptDialog.dismiss();
            }
        }, new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //退出
                promptDialog.dismiss();
            }
        });
    }




    @OnClick({R.id.tv_switchcartype})
    public void onClick(View v){
        switch (v.getId()){
            case  R.id.tv_switchcartype:
                if(INPUT_CARNUM==0){
                    INPUT_CARNUM=1;
                    tv_divider7.setVisibility(View.VISIBLE);
                    inputbox8.setVisibility(View.VISIBLE);
                    inputbox8.setText("");
                    tv_switchcartype.setText("切换普通车牌");
                }else if(INPUT_CARNUM ==1){
                    INPUT_CARNUM=0;
                    tv_divider7.setVisibility(View.GONE);
                    inputbox8.setVisibility(View.GONE);
                    tv_switchcartype.setText("切换新能源车牌");
                }
                break;
        }
    }

    @Override
    protected void initData() {
        Intent intent = getIntent();
        parking_id = intent.getStringExtra("parking_id");
        getRentParkingRule(parking_id);
        getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
        //输入车牌完成后的intent过滤器
        finishFilter = new IntentFilter(INPUT_LICENSE_COMPLETE);
        receiver =  new  BroadcastReceiver() {
            @Override
            public   void  onReceive(Context context, Intent intent) {
                String license = intent.getStringExtra(INPUT_LICENSE_KEY);
                Log.e("tag2",license);
                if(license != null && license.length() > 0){
                    builder = new AlertDialog.Builder(MonthlyPayActivity.this);
                    builder.setMessage("车牌号为:" + license);
                    car_no = license;
                    alertDialog = builder.create();
                    alertDialog.setCancelable(true);
                    alertDialog.show();
                }

            }
        };
        this.registerReceiver(receiver, finishFilter);

    }
    public static void startAction(Context context, String parking_id) {
        Intent intent = new Intent(context, MonthlyPayActivity.class);
        intent.putExtra("parking_id", parking_id);
        context.startActivity(intent);
    }
    public void refreshData(){
        lv_monthlypay.setVerticalScrollBarEnabled(false);
        lv_monthlypay.setDividerHeight(3);
        lv_monthlypay.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//                Log.e("tag","car_no = "+car_no+"   ,rule_id = "+list.get(position).parking_rule_id+"  ,parkingid = "+parking_id);

                createParkingrentOrder(list.get(position).parking_rule_id,parking_id,car_no);
            }
        });
        adapter = new ParkingRentRulesAdapter(this, R.layout.item_monthlyrent);
        lv_monthlypay.setAdapter(adapter);
        adapter.getDataList().addAll(list);
        adapter.notifyDataSetChanged();
    }

    /**
     * 获取停车场费用规则列表
     * @param parking_id
     */
    public void getRentParkingRule(String parking_id){
        new LHttpLib().getMonthlyrentRule(mContext,memberId,parking_id,new BaseLHttpHandler(mContext,true,true){
            @Override
            public void onSuccess(JSONStatus jsonStatus) {
                super.onSuccess(jsonStatus);
                JSONArray array = jsonStatus.data.optJSONArray("parking_rule_List");
                Log.e("tag","array = "+array.length());
                if(array!=null){
                    list = new ArrayList<MonthlyRentRuleInfo>();
                    for (int i=0,length = array.length();i<length;i++){
                        MonthlyRentRuleInfo ruleInfo = new MonthlyRentRuleInfo().parse(array.optJSONObject(i));
                        list.add(ruleInfo);
                    }
                }
                refreshData();
            }
        });
    }

    /**
     * 提交停车场费用规则请求
     */
    private void createParkingrentOrder(final String parking_rule_id,final String parking_id,final String car_no){
        new LHttpLib().createParkingrentOrder(mContext,memberId,parking_rule_id,parking_id,car_no,new BaseLHttpHandler(mContext,true,true){
            @Override
            public void onSuccess(JSONStatus jsonStatus) {
                super.onSuccess(jsonStatus);
              Log.e("tag","create = "+jsonStatus.data.toString());
                Intent intent = new Intent(mContext,MonthlyRentCostActivity.class);
                intent.putExtra("obj",jsonStatus.data.toString());
                startActivity(intent);
            }
        });
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        MonthlyPayActivity.this.unregisterReceiver(receiver);
    }
}
